package org.zoomdev.zoom.hash;

import com.google.common.hash.Hashing;

import java.nio.charset.StandardCharsets;

public enum HashAlgorithm {
    MURMUR3_32 {
        @Override
        int hash(String key) {
            return Hashing.murmur3_32().hashString(key, StandardCharsets.UTF_8).asInt() & 0x7FFFFFFF;
        }
    },
    FNV1_32 {
        @Override
        int hash(String key) {
            final int FNV_32_INIT = 0x811c9dc5;
            final int FNV_32_PRIME = 0x01000193;
            int hash = FNV_32_INIT;
            for (byte b : key.getBytes(StandardCharsets.UTF_8)) {
                hash ^= b;
                hash *= FNV_32_PRIME;
            }
            return hash & 0x7FFFFFFF;
        }
    };

    abstract int hash(String key);
}